cc.Class({
    extends: cc.Component,

    properties: {
        root: cc.Node,
        bg: cc.Node,
        sceneLayer: cc.Node,
        lobbyView: cc.Node,
        gameView: cc.Node,
        alertLayer: cc.Node,
        loadDataMask: require('GameMask'),
        rankMask: require('GameMask'),
        groupRankPanel: require('GroupRankPanel'),
        friendRankPanel: require('GroupRankPanel'),
        worldRankPanel: require('WorldRankPanel'),
        worldHistoryPanel: require('WorldHistoryPanel'),
        loadInfo: require('LoadInfo'),
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this._mediator = require('MainMediator');

        let UIManager = require('UIManager');
        
        UIManager.startup(require('AppInfo').wx);
        this.root.y = UIManager.getRootY();
        this.bg.height = UIManager.getRealH();
        this.bg.y = - UIManager.getRootY();
        this.alertLayer.y = - this.root.y;
        this.loadInfo.node.y = -UIManager.getRealH() * 0.4;

        let m = this._mediator;

        this.friendRankPanel.btnPre.node.on('click', m.preFriendRankHandler, m);
        this.friendRankPanel.btnNext.node.on('click', m.nextFriendRankHandler, m);
        this.groupRankPanel.btnPre.node.on('click', m.preGroupRankHandler, m);
        this.groupRankPanel.btnNext.node.on('click', m.nextGroupRankHandler, m);
        this.friendRankPanel.btnReturn.on('click', m.main_hideRankHandler, m);
        this.groupRankPanel.btnReturn.on('click', m.main_hideRankHandler, m);
        this.worldRankPanel.btnReturn.on('click', m.main_hideWorldHandler, m);
        this.worldRankPanel.btnHistory.on('click', m.worldHistoryHandler, m);
        this.worldRankPanel.btnPre.node.on('click', m.main_preWorldRankHandler, m);
        this.worldRankPanel.btnNext.node.on('click', m.main_nextWorldRankHandler, m);
        this.worldHistoryPanel.btnReturn.on('click', this.worldHistoryCloseHandler, this);
        this.worldHistoryPanel.btnPre.node.on('click', m.main_worldHistoryPreHandler, m);
        this.worldHistoryPanel.btnNext.node.on('click', m.main_worldHistoryNextHandler, m);
    },

    start () {
        this._mediator.register(this);
    },

    onHide: function () {
    },

    toLobbyScene: function () {
        this.sceneLayer.removeAllChildren(false);
        this.sceneLayer.addChild(this.lobbyView);
        this.onHide();
    },

    toGameScene: function () {
        this.sceneLayer.removeAllChildren(false);
        this.sceneLayer.addChild(this.gameView);
        this.onHide();
    },

    /** 显示加载数据 */
    showLoadData: function (info, state) {
        if (!this.loadDataMask) return;

        this.loadDataMask.show(1);
        this.loadInfo.show(info, state);
    },

    /** 隐藏加载数据 */
    hideLoadData: function () {
        if (!this.loadDataMask) return;

        this.loadDataMask.hide();
        this.loadInfo.hide();
    },

    hideRankPanel: function () {
        this.rankMask.hide();
        this.friendRankPanel.hide();
        this.groupRankPanel.hide();
        this.worldRankPanel.hide();
    },

    showGroupRankPanel: function (data) {
        this.rankMask.show();
        this.friendRankPanel.hide();
        this.worldRankPanel.hide();
        this.groupRankPanel.show(data);
    },

    showFriendRankPanel: function (data) {
        this.rankMask.show();
        this.groupRankPanel.hide();
        this.worldRankPanel.hide();
        this.friendRankPanel.show(data);
    },

    showWorldRankPanel: function (data) {
        this.rankMask.show();
        this.groupRankPanel.hide();
        this.friendRankPanel.hide();
        this.worldRankPanel.show();
    },

    setWorldRankData: function (data) {
        this.worldRankPanel.setData(data);
    },

    setWorldRankListData: function (data) {
        this.worldRankPanel.setListData(data);
    },

    showWorldHistory: function () {
        this.worldRankPanel.node.active = false;
        this.worldHistoryPanel.show();
    },

    worldHistoryCloseHandler: function () {
        this.worldRankPanel.node.active = true;
        this.worldHistoryPanel.hide();
    },

    setBestHistory: function (data) {
        this.worldHistoryPanel.setBestHistory(data);
    },

    setMyBest: function (data) {
        this.worldHistoryPanel.setMyHistory(data);
    },

    // update (dt) {},
});
